import 'package:flutter/material.dart';

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          SliverToBoxAdapter(
            child: Container(
                width: double.maxFinite, height: 160, color: Colors.red),
          ),
          const MyPersistentHeader(), // 使用自定义的SliverPersistentHeader组件
          SliverList(
            // 其他Sliver组件，例如列表项等...
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return ListTile(title: Text('列表项 $index')); // 示例列表项
              },
              childCount: 40, // 示例列表项数量
            ),
          ),
        ],
      ),
    );
  }
}

class MyPersistentHeader extends StatelessWidget {
  const MyPersistentHeader({super.key});

  @override
  Widget build(BuildContext context) {
    return SliverPersistentHeader(
      pinned: true, // 使头部固定在顶部  是否固定头部。设为true时，头部在滑动到顶部时会固定在顶部。
      floating: false, // 是否浮动头部。设为true时，头部会在滚动时立即显示，而不是等到到达视口顶部才显示。
      delegate: PersistentHeaderBuilder(
        min: 120.0, // 最小高度
        max: 120.0, // 最大高度
        builder: (BuildContext context, double offset) {
          return Container(
            color: Colors.blue, // 背景颜色
            child: const Center(
              child: Text(
                '固定头部', // 显示的文本
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
            ),
          );
        },
      ),
    );
  }
}

class PersistentHeaderBuilder extends SliverPersistentHeaderDelegate {
  final double min;
  final double max;
  final Widget Function(BuildContext context, double offset) builder;

  PersistentHeaderBuilder(
      {this.min = 80, this.max = 120, required this.builder})
      : assert(max >= min);

  @override
  double get maxExtent => max;

  @override
  double get minExtent => min;

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return builder(context, shrinkOffset);
  }

  @override
  bool shouldRebuild(covariant PersistentHeaderBuilder oldDelegate) =>
      max != oldDelegate.max ||
      min != oldDelegate.min ||
      builder != oldDelegate.builder;
}

///
class MyApp1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          // 使用SliverAppBar
          SliverAppBar(
            title: Text('滑动显示AppBar'),
            backgroundColor: Colors.white,
            floating: false,
            pinned: true, // 固定AppBar
            expandedHeight: 200.0, // AppBar完全展开的高度
            snap: false,
            flexibleSpace: FlexibleSpaceBar(
              background: Image.network(
                'https://inews.gtimg.com/news_bt/OBkbmPLeWLy4IM4oUDGvOIqSDSZ9lYOtW3qSXCYh78KXcAA/1000', // 背景图片
                fit: BoxFit.cover,
              ),
            ),
          ),
          // 使用SliverList显示列表项
          SliverList(
            delegate: SliverChildBuilderDelegate(
                  (BuildContext context, int index) {
                return ListTile(title: Text('项目 $index'));
              },
              childCount: 100, // 假设有100个列表项
            ),
          ),
        ],
      ),
    );
  }
}


